package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.TaskOrderMapper;
import com.stylefeng.guns.modular.netcar.model.TaskOrder;
import com.stylefeng.guns.modular.netcar.service.ITaskOrderService;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 任务单-订单信息 Service 实现 
 * @author sasazaixian
 * @Date 2018-10-25 18:14
 */
@Service
public class TaskOrderServiceImpl extends ServiceImpl<TaskOrderMapper,TaskOrder> implements ITaskOrderService {

    @Resource
    private TaskOrderMapper taskOrderMapper;

    /**
     * 删除任务单-订单信息
     * @param taskOrder
     * @return     
     */
    @Transactional
    public int deleteTaskOrder(TaskOrder taskOrder,String token)throws Exception{
        if(taskOrder==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.taskOrderMapper.deleteTaskOrder(taskOrder);
    }
    /**
     * 批量删除任务单-订单信息
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchTaskOrder(String[] ids,String token)throws Exception{
        return this.taskOrderMapper.deleteBatchTaskOrder(ids);
    }
    /**
     * 查询任务单-订单信息列表
     * @param taskOrder 任务单-订单信息对象
     * @return
     */
    public List<TaskOrder> selectListTaskOrder(TaskOrder taskOrder,String token)throws Exception{
        return  this.taskOrderMapper.selectListTaskOrder(taskOrder);
    }
    /**
	 * 分页查询任务单-订单信息列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListTaskOrder(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(taskOrderMapper.selectListTaskOrder(page,(TaskOrder)pager.getObj()));
        List<TaskOrder> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param taskOrder 任务单-订单信息对象
     * @return 
     */
    public  List<TaskOrder> selectListTaskOrderForUpdate(TaskOrder taskOrder)throws Exception{
        return  this.taskOrderMapper.selectListTaskOrderForUpdate(taskOrder);
    }
    /**
     * 通过主键id查询的任务单-订单信息列表
     * @param id 主键id
     * @return     
     */
    public  TaskOrder findTaskOrderById(String id,String token)throws Exception{
        return  this.taskOrderMapper.findTaskOrderById(id);
    }

    /**
     * 新增任务单-订单信息数据
     * @param taskOrder  任务单-订单信息对象
     * @return
     */
    @Transactional
    public int insertTaskOrder(TaskOrder taskOrder,String token) throws Exception { 
        if(taskOrder!=null && (taskOrder.getToId()==null || taskOrder.getToId().trim().length()<10)){
            taskOrder.setToId(IdWorker.get32UUID());
        }  
        return this.taskOrderMapper.insertTaskOrder(taskOrder);
    }
    
    /**
     * 更新任务单-订单信息数据
     * @param taskOrder 任务单-订单信息对象
     * @return     
     */
    @Transactional
    public int updateTaskOrder(TaskOrder taskOrder,String token) throws Exception {     	
        return  this.taskOrderMapper.updateTaskOrder(taskOrder);
    }    
    /**
     * 查询任务单-订单信息返回key-Value属性
     * @param taskOrder 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsTaskOrder(TaskOrder taskOrder,String token) throws Exception {
        List<TaskOrder> listTaskOrder=this.taskOrderMapper.selectListTaskOrder(taskOrder);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(TaskOrder item:listTaskOrder){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getToId());
            opts.setValue(item.getToId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
